<cfsilent>
<cfinclude template="jsonencode.cfm">
<cfinclude template="jsondecode.cfm">

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true" />

<cfparam name="url.action" default="">
<cfparam name="url.data" default="">
<cfset request.dsource = "dojotreeDSN">

<cffunction name="dumpvar" output="true" access="private">
	<cfargument name="dumpThis" required="true">
	<cfargument name="and_abort" default="false">
	<cfdump var="#dumpThis#">
	<cfif and_abort><cfabort></cfif>
</cffunction>

<cfinclude template="treeActions.cfm">

<cffunction name="getParents" output="false" access="private">
	<cfargument name="tree_ID" required="true">	
	<cfset var daParents = getChildrenQry(tree_ID=tree_ID,parentID="")>
	<cfsavecontent variable="parentDojoDivs">
	<cfoutput query="daParents">
	<div dojoType="TreeNodeV3" widgetId="#widgetID#" objectId="treenode" title="#title#" isFolder="true">
	</div>
	</cfoutput>
	<cfif daParents.recordcount eq 0>
	<cfoutput>
	<div dojoType="TreeNodeV3" widgetId="0" objectId="treenode" title="Initial Item (not saved)" isFolder="true">
	</div>
	</cfoutput>
	</cfif>
	</cfsavecontent>
	<cfreturn parentDojoDivs>
</cffunction>


<cfscript>
function getChildren(tree_ID,parentID) {
	var theKidsQry = getChildrenQry(tree_ID,parentID);
	var kids = "";
	var kidInfo = "";
	for (x = 1; x lte theKidsQry.recordcount; x=x+1) {
		kidInfo = "";
		kidInfo = listAppend(kidInfo,'title:"#theKidsQry["title"][x]#"');
		kidInfo = listAppend(kidInfo,'widgetId:"#theKidsQry["widgetId"][x]#"');
		//kidInfo = listAppend(kidInfo,'objectId:"TreeNodeV3"');
		if (val(theKidsQry["hasKids"][x]) neq 0) {
			kidInfo = listAppend(kidInfo,'isFolder:true');
		}
		kidInfo = listAppend(kidInfo,'index:#val(theKidsQry["tree_item_order"][x])#');
		kidInfo = "{#kidInfo#}";
		kids = listAppend(kids,kidInfo);
	}
	return "([#kids#])";	
}

function editLabelSave(editLabelData) {
	var moveWidget = editLabelData["node"]["widgetId"];
	var newTitle = editLabelData["newContent"];
	return setNode(nodeID=moveWidget,title=newTitle);
	//return "({title:""#newTitle#""})";
}

function createChild(childdata) {
	var newParentID = childdata["parent"]["widgetId"];
	// var sugTitle = childdata["data"]["suggestedTitle"];
	var newTitle = childdata["data"]["title"];
	var newParentTreeID = childdata["tree"]["widgetId"];
	newParentTreeID = replace(newParentTreeID,"tree","","all");
	return setNode(nodeID=0,title=newTitle,objectID="TreeNodeV3",parentID=newParentID,treeID=newParentTreeID);
}

function removeNode(nodedata) {
	var widgetId = nodedata["node"]["widgetId"];
	return deleteNode(widgetId);
}

function moveNode(movedata) {
	var moveWidget = movedata["child"]["widgetId"];
	var newIndex = movedata["newIndex"];
	var newParentID = movedata["newParent"]["widgetId"];
	var newObjectID = movedata["newParent"]["objectId"];
	var newParentTreeID = movedata["newParentTree"]["widgetId"];
	return setNode(nodeID=moveWidget,objectID=newObjectID,index=newIndex,parentID=newParentID,treeID=newParentTreeID);
}

function runAction(action,data) {
	var daData = "";
	setServerDebug();
	switch ("#LCase(action)#") {
		case "getChildren":
			daData = jsondecode(data);
			daChilln = getChildren(daData["tree"]["widgetId"],daData["node"]["widgetId"]);
	    //dumpvar(daChilln,true);
			addServerDebug(daChilln);
	    return daChilln;
		case "createChild":
			daData = jsondecode(data);
			addServerDebug(daData);
			return createChild(daData);
		case "removeNode":
			daData = jsondecode(data);
			addServerDebug('Removed Node');
			return removeNode(daData);
		case "move":
			daData = jsondecode(data);
			addServerDebug('Move');
			return moveNode(daData);
		case "editLabelSave":
			daData = jsondecode(data);
			editLabelSaveRslt = editLabelSave(daData);
			addServerDebug(editLabelSaveRslt);
			return editLabelSaveRslt;
		default:
	  	return;
	}
}
</cfscript>

<cffunction name="setServerDebug">
	<cfargument name="append" default="false">
	<cfsavecontent variable="daDump">
		<cfoutput>#url.action#</cfoutput>
		<cfdump var="#jsondecode(url.data)#">
	</cfsavecontent>
	<cfif append>
		<cfset session.funstuff = session.funstuff & daDump>
	<cfelse>
		<cfset session.funstuff = daDump>
	</cfif>
</cffunction>

<cffunction name="addServerDebug">
	<cfargument name="daVar" required="true">
	<cfargument name="append" default="true">
	<cfsavecontent variable="daDump">
		<cfdump var="#daVar#">
	</cfsavecontent>
	<cfif append>
		<cfset session.funstuff = session.funstuff & daDump>
	<cfelse>
		<cfset session.funstuff = daDump>
	</cfif>
</cffunction>

</cfsilent>
<cfif structKeyExists(url,"runaction")>
	<cfoutput>#runAction(url.action,url.data)#</cfoutput>
</cfif>